package com.telecomyt.jwportal.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.utils.DisplayUtil;

/**
 * @author created by chengDa
 * @time 2020/10/13  11:35
 * @description
 */
public class AvatarUtils {

    public static void setUserAvatar(Context context, ImageView imageView, String mName, String headPath) {
        if (!TextUtils.isEmpty(headPath)) {
            GlideImageLoader.load(context, headPath, getNameAvatarDrawable(context, TextUtils.isEmpty(mName) ? "未知" : mName, R.color.white, R.color.blueF4),
                    getNameAvatarDrawable(context, TextUtils.isEmpty(mName) ? "未知" : mName, R.color.white, R.color.blueF4),
                    getNameAvatarDrawable(context, TextUtils.isEmpty(mName) ? "未知" : mName, R.color.white, R.color.blueF4),
                    imageView, new GlideImageLoader.ErrorListener() {
                        @Override
                        public void error() {
                            loadName(context, mName, imageView);
                        }
                    });
        } else {
            loadName(context, mName, imageView);

        }
    }


    /**
     * @date: 2020/10/13
     * @author: chengDa
     * @description: 加载name
     */
    private static void loadName(Context context, String mName, ImageView imageView) {
        if (TextUtils.isEmpty(mName)) {
            mName = "未知";
        }
        Drawable nameAvatarDrawable = getNameAvatarDrawable(context, mName, R.color.white, R.color.blueF4);
        imageView.setImageDrawable(nameAvatarDrawable);
    }

    public static Drawable getNameAvatarDrawable(Context var0, String var1, int textcolor, int bgcolor) {
        int var4 = DisplayUtil.dip2px(var0, (float) var0.getResources().getDimensionPixelOffset(R.dimen.width));
        return TextDrawable.builder().beginConfig().textColor(var0.getResources().getColor(textcolor)).width(var4).height(var4).fontSize(var4 / 3).endConfig().buildRound(getDrawName(var1), var0.getResources().getColor(bgcolor));
    }

    public static String getDrawName(String var0) {
        if (TextUtils.isEmpty(var0)) {
            return "";
        } else {
            String var1 = var0.replaceAll(" ", "").trim();
            if (var1.length() > 2) {
                var1 = var1.substring(var1.length() - 2, var1.length());
            }

            return var1;
        }
    }
}
